const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    content: '',
    addr: {},
    image: [],
    video: {},
    canIUse: wx.canIUse('button.open-type.openSetting')
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.chooseFile()
    this.getLocation()
    this.getColumn()
  },
  changeContent: function (e) {
    this.setData({
      content: e.detail.value
    })
  },
  chooseAddr: function () {
    wx.chooseLocation({
      success: (res) => {
        console.log(res)
        this.setData({
          addr: res
        })
      },
      fail: (e) => {
        if (!this.data.addr.name) {
          app.toast('选择地址后才能发帖')
        }
      }
    })
  },
  chooseFile: function () {
    if (this.options.type == 0) {
      if (this.data.image.length >= 9) {
        app.toast('最多只能上传9张照片')
      } else {
        this.chooseImage()
      }
    } else {
      if (this.data.video.tempFilePath) {
        wx.showModal({
          title: '只能上传一个视频',
          content: '是否重新上传？',
          success: (res) => {
            if (res.confirm) {
              this.setData({
                video: {}
              })
              this.chooseVideo()
            }
          }
        })
      } else {
        this.chooseVideo()
      }
    }
  },
  chooseImage: function () {
    wx.chooseImage({
      sourceType: ['camera'],
      success: (res) => {
        let url = [...this.data.image, ...res.tempFilePaths]
        this.setData({
          image: url
        })
      },
    })
  },
  chooseVideo: function () {
    wx.chooseVideo({
      sourceType: ['camera'],
      maxDuration: 60,
      success: (res) => {
        console.log(res)
        this.setData({
          video: res
        })
      }
    })
  },
  deleteFile: function (e) {
    let index = e.currentTarget.dataset.index,
      image = this.data.image;
    image.splice(index, 1);
    this.setData({
      image: image
    })
  },
  getLocation: function () {
    wx.getLocation({
      type: 'wgs84',
      success: (res) => {
        wx.request({
          url: 'https://apis.map.qq.com/ws/geocoder/v1/?location=' + res.latitude + ',' + res.longitude + '&key=6U6BZ-TJXWP-G5ADN-L3S6W-2G73S-7QBV2&coord_type=1',
          success: (res) => {
            if (res.data.status === 0) {
              let addr = {
                address: res.data.result.address,
                latitude: res.data.result.location.latitude,
                longitude: res.data.result.location.longitude,
                name: res.data.result.formatted_addresses.recommend
              }
              this.setData({
                addr: addr
              })
            }
          }
        })
      },
      fail: (res) => {
        if (!this.data.canIUse) {
          wx.openSetting({
            success: (res) => {
              this.getLocation()
            }
          })
        }
      }
    })
  },
  /**
 * 获取栏目信息
 * @params type
 */
  getColumn: function (isLoading) {
    app.get('posts/getColumn', { type: 1 })
      .then(res => {
        this.setData({
          columnList: res,
          column: res[0].id
        })
      })
  },
  changeColumn: function (e) {
    let id = e.currentTarget.dataset.id
    this.setData({
      column: id
    })
  },
  lookImg: function (e) {
    app.imgs(e.currentTarget.dataset.urls, e.currentTarget.dataset.src)
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})